package com.ncr.travel.appmgr.client.view;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.ncr.travel.appmgr.client.ConfigurationItem;
import com.ncr.travel.appmgr.client.mvp.EventBus;
import com.ncr.travel.appmgr.client.mvp.Presenter;
import com.ncr.travel.appmgr.client.view.FunctionWidget.FunctionAppMgrWindowUiBinder;
import com.ncr.travel.appmgr.model.shared.ErrorText;
import com.ncr.travel.appmgr.model.shared.ScreenConfiguration;
import com.ncr.travel.appmgr.model.shared.Text;
import com.ncr.travel.appmgr.model.shared.TextEntry;

/**
 * @author ng185038
 * 
 */
public class TextWidget extends Composite implements TextView {

    

    private static TextAppMgrWindowUiBinder uiBinder = GWT.create(TextAppMgrWindowUiBinder.class);

    @UiTemplate("TextView.ui.xml")
    interface TextAppMgrWindowUiBinder extends UiBinder<Widget, TextWidget> {
    }
    
    
    @UiField
	ScrollPanel textScrollPanel;
	@UiField
	FlowPanel mainPanel;
	private FlowPanel labelTextPnl;
	private FlowPanel butttonTextPnl;
	private FontOptionsView fontOptionsView;
	private ConfigEditorView configEditorView;
	@UiField
	HTMLPanel topLevelPanel;
	private EventBus eventBus;
	private ChangeHandler textConfigChangeHandler;
	private FocusHandler textConfigFocusHandler;
	private ClickHandler fontOptionsClickHandler;
	private Presenter<TextView> presenter;
	private ScreenConfiguration screenConfiguration;

	@Inject
	public TextWidget(EventBus eventBus) {
		super();
	    initWidget(uiBinder.createAndBindUi(this));
		System.out.println("Constructing TextWidget");
		this.eventBus = eventBus;
	    configEditorView = new ConfigEditorWidget("Are you sure you want to confirm text configuration changes?",
	                eventBus);
	    configEditorView.setConfigurationItem(ConfigurationItem.TEXT);
	    configEditorView.setScreenConfiguration(screenConfiguration);
	    topLevelPanel.add(configEditorView.asWidget());

	}

	public void setScreenConfiguration(final ScreenConfiguration screenConfiguration) {
		this.screenConfiguration = screenConfiguration;
	}

	@Override
	public void updateTextConfigurations(List<Text> textConfigs) {
		fontOptionsView = null;
		mainPanel.clear();
		if (textConfigs == null || textConfigs.isEmpty()) {
			return;
		}

		/*
		 * Font Options View cannot be moved to UiBinder because it relays on mainpanel, 
		 * fontOptionsClickHandler and textConfigChangeHandler, which are not set in Text Widget constructor.
		 */
		fontOptionsView = new FontOptionsWidget(mainPanel, fontOptionsClickHandler, textConfigChangeHandler);
		mainPanel.add(fontOptionsView.asWidget());
		mainPanel.add(new HTML("<hr>"));

		/*
		 * LabelTexPnl may not be added to mainPanel, so cannot change to UiBinder
		 **/
		labelTextPnl = new FlowPanel();
		Label labelTextHeader = new Label("Instructional Text / Label");
		labelTextHeader.setStyleName("confLabelHeader");
		
		labelTextPnl.add(labelTextHeader);
        /*
         * butttonTextPnl may not be added to mainPanel, so cannot change to UiBinder
         **/
		butttonTextPnl = new FlowPanel();
		Label btnTextHeader = new Label("Button Text");
		btnTextHeader.setStyleName("confLabelHeader");
		butttonTextPnl.add(btnTextHeader);
		for (int index = 0; index < textConfigs.size(); index++) {

			Label textConfiglbl = new Label();
			textConfiglbl.setStyleName("confLabelRegular");
			Label errorCodelbl = null;
			Text textConfig = textConfigs.get(index);
			if (textConfig instanceof TextEntry) {
				textConfiglbl.setText(((TextEntry) textConfig).getValue());
			} else if (textConfig instanceof ErrorText) {
				ErrorText errorText = (ErrorText) textConfig;
				textConfiglbl.setText(errorText.getMessage());
				errorCodelbl = new Label("Error " + errorText.getCode());
				errorCodelbl.setStyleName("errorCodeLabel");
			}
			TextBoxBase textbase;
			if (textConfig.isMultiLine()) {
				textbase = new TextArea();
				((TextArea) textbase).setVisibleLines(3);
	            textbase.setHeight("75px");
			} else {
				textbase = new TextBox();
	            textbase.setHeight("25px");
			}
			textbase.getElement().setId(textConfig.createElementId());

			textbase.setWidth("77%");
			textbase.setStyleName("textBase");

			textbase.addChangeHandler(textConfigChangeHandler);
			textbase.addFocusHandler(textConfigFocusHandler);
			
			if (textConfig.getCategory() == null) {
				continue;
			}
			switch (textConfig.getCategory()) {
			case LABEL: {
				if (errorCodelbl != null) {
					labelTextPnl.add(errorCodelbl);
				}
				labelTextPnl.add(textConfiglbl);
				labelTextPnl.add(textbase);
				labelTextPnl.add(new HTML("<hr>"));
				break;
			}
			case BUTTON: {
				if (errorCodelbl != null) {
					butttonTextPnl.add(errorCodelbl);
				}
				butttonTextPnl.add(textConfiglbl);
				butttonTextPnl.add(textbase);
				butttonTextPnl.add(new HTML("<hr>"));
				break;
			}
			}
		}//end for loop
		// topLevelPanel.add(new HTML("<hr>"));

		if (labelTextPnl.getWidgetCount() > 1) {
			mainPanel.add(labelTextPnl);
		} else {
			labelTextPnl = null;
		}
		if (butttonTextPnl.getWidgetCount() > 1) {
			mainPanel.add(butttonTextPnl);
		} else {
			butttonTextPnl = null;
		}

	}

	@Override
	public FontOptionsView getFontOptionsView() {
		return this.fontOptionsView;
	}

	@Override
	public ConfigEditorView getConfigEditorView() {
		return configEditorView;
	}

	@Override
	public void setPresenter(final Presenter<TextView> presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setChangeHandler(final ChangeHandler changeHandler) {
		this.textConfigChangeHandler = changeHandler;
	}

	@Override
	public void setFocusHandler(final FocusHandler focusHandler) {
		this.textConfigFocusHandler = focusHandler;
	}

	@Override
	public void setClickHandler(final ClickHandler clickHandler) {
		this.fontOptionsClickHandler = clickHandler;
	}
}